﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class OneCanteenSummaryReportDetailController : Controller
    {
        private OneCanteenSummaryReportDetailInfo fundinfo = new OneCanteenSummaryReportDetailInfo();
        public ActionResult OneCanteenSummaryReportDetailIndex()
        {
            return View();
        }

        public ActionResult GetList(PageVM pvm)
        {
            OneCanteenSummaryReportDetailInfo info = new OneCanteenSummaryReportDetailInfo();
            string condition = pvm.query;
            string strWhere = "";
            info.StartTime = condition.Split(',')[0].ToString();
            info.EndTime = condition.Split(',')[1].ToString();
            //info.PNAME = condition.Split(',')[2].ToString();
            if (info.StartTime != "")
            {
                strWhere += " and a.RDateTime>='" + info.StartTime + "'";
            }
            if (info.EndTime != "")
            {
                strWhere += " and a.RDateTime<='" + info.EndTime + "'";
            }
            //if (info.PNAME != "")
            //{
            //    strWhere += " and f.PNAME like '%" + info.PNAME + "%'";
            //}
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            int count;
            DataSet list = fundinfo.BindList(pvm.rp, pvm.page, strWhere);
            jsonFlexiGridData.total = fundinfo.GetListAllCount(strWhere);
            List<string> v;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                #region
                v = new List<string>();
                v.Add(item["MenuId"].ToString());
                v.Add(item["MenuName"].ToString());
                v.Add(item["Counts"].ToString());
                v.Add(item["Price"].ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item["MenuId"].ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 一食堂汇总报表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string StartTime, string EndTime)
        {
            return EContent(StartTime, EndTime);
        }

        public ExcelContentResult EContent(string startTime, string endTime)
        {
            OneCanteenSummaryReportDetailInfo info = new OneCanteenSummaryReportDetailInfo();
            string strWhere = "";
            info.StartTime = startTime;
            info.EndTime = endTime;
            //info.PNAME = pname;
            if (info.StartTime != "")
            {
                strWhere += " and a.RDateTime>='" + info.StartTime + "'";
            }
            if (info.EndTime != "")
            {
                strWhere += " and a.RDateTime<='" + info.EndTime + "'";
            }
            //if (info.PNAME != "")
            //{
            //    strWhere += " and f.PNAME like '%" + info.PNAME + "%'";
            //}
            StringBuilder sBuilder = new StringBuilder();
            DataSet list = fundinfo.GetListAll(strWhere);
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='4' style='font-size:25px; text-align:center;'>一食堂详细统计表</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td colspan='4' style='font-size17px;'>时间：" + startTime + "到" + endTime + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td>餐次</td>");
            sBuilder.Append("<td>菜单名称</td>");
            sBuilder.Append("<td>份数</td>");
            sBuilder.Append("<td>总金额</td>");
            sBuilder.Append("</tr>");

            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                sBuilder.Append("<td>" + item["PeriodName"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["MenuName"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["Counts"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["Price"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("</tr>");
            list = fundinfo.GetOneHJ(strWhere);
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                sBuilder.Append("<td colspan='2'>" + item["PeriodName"].ToString() + "合计：</td>");
                sBuilder.Append("<td>" + item["Counts"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["Price"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("<td></td>");
            sBuilder.Append("</tr>");
            list = fundinfo.GetAllHJ(strWhere);
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td colspan='2'>总合计：</td>");
            sBuilder.Append("<td>" + list.Tables[0].Rows[0]["Counts"].ToString() + "</td>");
            sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(list.Tables[0].Rows[0]["Price"]), 1, MidpointRounding.AwayFromZero) + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "yishitangxiangxihuizong";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
